package com.spring.mao.exception;

import com.spring.mao.common.Result;
import com.spring.mao.common.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MaoExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(MaoExceptionHandler.class);


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handler(Exception e){

        if (e instanceof MaoException){
            MaoException maoException = (MaoException)e;
            return ResultUtil.error(maoException.getCode(),maoException.getMessage());
        }else {
            logger.error("系统异常{}",e);
            return ResultUtil.error(ResultEnum.UNKONW_ERROR.getCode(),ResultEnum.UNKONW_ERROR.getMsg());
        }

    }


}
